<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>发射机节目列表</title>
    <link rel="stylesheet" th:href="@{../../component/pear/css/pear.css}"/>
    <style>
        .inputstyle {
            width: 144px;
            height: 41px;
            cursor: pointer;
            font-size: 30px;
            outline: medium none;
            position: absolute;
            filter: alpha(opacity=0);
            -moz-opacity: 0;
            opacity: 0;
            left: 0px;
            top: 0px;
        }
    </style>
</head>

<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <div class="col-sm-13 search-collapse">
            <form class="layui-form" id="formId">
                <div class="layui-form-item">
                    <label class="layui-form-label">台站ID：</label>
                    <div class="layui-input-inline">
                        <input class="layui-input"  name="stationCode" placeholder="请输入台站ID" type="text"/>
                    </div>
                    <label class="layui-form-label" style="width: 7%">发射机ID：</label>
                    <div class="layui-input-inline">
                        <input class="layui-input" name="transCode" placeholder="请输入发射机ID" type="text"/>
                    </div>
                    <label class="layui-form-label" style="width: 7%">任务开始时间：</label>
                    <div class="layui-input-inline">
                        <input type="text" class="layui-input" id="sendDateTime"  name="sendDateTime"  placeholder="yyyy-MM-dd HH:mm:ss" >
                    </div>
                    <button class="pear-btn pear-btn-md pear-btn-primary" lay-filter="myTable-query" lay-submit>
                        <i class="layui-icon layui-icon-search"></i>
                        查询
                    </button>
                    <button class="pear-btn pear-btn-md" type="reset">
                        <i class="layui-icon layui-icon-refresh"></i>
                        重置
                    </button>
                </div>
            </form>
        </div>
    </div>
</div>
<div class="layui-card">
    <div class="layui-card-body">
        <table id="myTable" lay-filter="myTable"></table>
    </div>
</div>

<script th:src="@{../../component/layui/layui.js}"></script>
<script th:src="@{../../component/pear/pear.js}"></script>

<script type="text/html" id="power-toolbar">
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
        <i class="layui-icon layui-icon-add-1"></i>
        新增
    </button>
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="down">
        模板下载
    </button>
    <input class="pear-btn pear-btn-primary pear-btn-md" type=button id=filebutton value="上传文件" onclick="browsefile.click()">
    <input type=file id=browsefile style="visibility:hidden" onchange="handleFileSelect()">
</script>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="detail">详情</a>
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-xs" lay-event="delete">删除</a>
</script>
<script>
    let upload;
    layui.use(['table', 'form', 'jquery', 'popup','laydate','element', 'util','layer','upload'], function () {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let popup = layui.popup;
        let laydate = layui.laydate;
        upload = layui.upload;
        let MODULE_PATH = "/system/program/";

        laydate.render({
            elem: '#sendDateTime',
            type: 'datetime',
            theme: 'grid',
            // value: getRecentDay(0),
        });
        setInterval(function () {
            table.reloadData('myTable');
        }, 10000);

        form.on('submit(myTable-query)', function (data) {
            table.reload('myTable', {where: data.field})
            return false;
        });

        let cols = [
            [
                {
                    title: '台站ID',
                    field: 'stationCode',
                    align: 'center'
                },
                {
                    title: '发射机ID',
                    field: 'transCode',
                    align: 'center'
                },
                {
                    title: '通道号',
                    field: 'taskId',
                    align: 'center',
                    sort: true
                },
                {
                    title: '开始时间',
                    field: 'sendDateTime',
                    align: 'center',
                    sort: true
                },
                {
                    title: '结束时间',
                    field: 'sendDateEndTime',
                    align: 'center',
                    sort: true
                },
                {
                    title: '工作频率(Hz,取值:3200~26100)',
                    field: 'channelFreq',
                    align: 'center',
                    templet: '#table-Register',
                    sort: true
                },
                {
                    title: '输出功率(W, 取值:0~+200)',
                    field: 'channelEqualization',
                    align: 'center'
                },
                {
                    title: '执行周期',
                    field: 'programPeriod',
                    templet: function (data) {
                        var array = data.programPeriod.split(",")
                        for (var i=0;i<array.length;i++){
                            if(array[i]=="1"){
                                array[i] = "星期一";
                            }
                            if(array[i]=="2"){
                                array[i] = "星期二";
                            }
                            if(array[i]=="3"){
                                array[i] =  "星期三";
                            }
                            if(array[i]=="4"){
                                array[i] =  "星期四";
                            }
                            if(array[i]=="5"){
                                array[i] =  "星期五";
                            }
                            if(array[i]=="6"){
                                array[i] =  "星期六";
                            }
                            if(array[i]=="7"){
                                array[i] =  "星期天";
                            }
                        }
                        return array;
                    },
                    align: 'center'
                },
                {
                    title: '操作',
                    toolbar: '#barDemo',
                    width: '15%'
                }
            ]
        ]

        table.render({
            elem: '#myTable',
            url: MODULE_PATH +'data',
            page: true,
            cols: cols,
            skin: 'line',
            toolbar: '#power-toolbar',
            defaultToolbar: [],
            cellMinWidth: 'auto',
            done: function (res, curr, count) {
            }
        });

        table.on('toolbar(myTable)', function(obj){
            if(obj.event === 'add'){
                var index = layer.open({
                    type: 2,
                    title: "添加发射机节目",
                    area: ['100%', '100%'],
                    fix: false,
                    maxmin: true,
                    shadeClose: true,
                    shade: 0.4,
                    skin: 'layui-layer-rim',
                    content: ['/system/view/program_add', "no"],
                    success: function (layero, index) {
                    }
                });
            }else if(obj.event === 'down'){
                // 对文件名进行解码。
                let filename = decodeURIComponent("xxxx.xls");

                // 创建a标签
                const link = document.createElement("a")
                try{
                    link.download = filename
                    // 2.使用接口返回的数据下载
                    link.href = "/system/program/down"
                    // 新标签中下载
                    link.target = "_blank"
                    link.style.display = "none"
                    // 将a标签添加到dom中
                    document.body.appendChild(link)
                    // 下载文件
                    link.click()
                }finally {
                    link.href && URL.revokeObjectURL(link.href)
                    // 从dom中移除a标签
                    document.body.removeChild(link)
                }

            }else if(obj.event === 'upload'){
                var file = document.getElementById('browsefile').files[0];
                var formData = new FormData();
                formData.append('file', file);
                var xhr = new XMLHttpRequest();
                xhr.open("POST", "/system/program/upload", true);
                xhr.onreadystatechange = function() {
                    if (xhr.readyState === 4 && xhr.status === 200) {
                        popup.success(data.msg);
                    }
                };
                xhr.send(formData);
            }
        });

        table.on('tool(myTable)', function(obj) {
            var rowIndex = $(obj.tr).attr("data-index");
            var data = obj.data;
            json = JSON.stringify(data);
            switch(obj.event) {
                case 'detail':
                    layer.open({
                        type: 2,
                        title: "节目详情页面",
                        area: ['100%', '100%'],
                        fix: false,
                        maxmin: true,
                        shadeClose: true,
                        shade: 0.4,
                        skin: 'layui-layer-rim',
                        content: ["/system/view/getProgramDetail", "no"],
                        success: function (layero, index) {
                            var body = layer.getChildFrame('body', index);
                            body.find("[name='stationCode']").val(data.stationCode);
                            body.find("[name='transCode']").val(data.transCode);
                            body.find("[name='taskId']").val(data.taskId);
                            body.find("[name='sendDateTime']").val(data.sendDateTime);
                            body.find("[name='sendDateEndTime']").val(data.sendDateEndTime);
                            body.find("[name='operType']").val(data.operType);
                            body.find("[name='channelFreq']").val(data.channelFreq);
                            body.find("[name='channelRfChState']").val(data.channelRfChState);
                            body.find("[name='channelTimeSlot']").val(data.channelTimeSlot);
                            body.find("[name='channelEqualization']").val(data.channelEqualization);
                            body.find("[name='channelRfChMod']").val(data.channelRfChMod);
                            body.find("[name='channelRfChBand']").val(data.channelRfChBand);
                            body.find("[name='channelRfChASource']").val(data.channelRfChASource);
                            body.find("[name='channelRfChAUrl']").val(data.channelRfChAUrl);
                            body.find("[name='sendTime']").val(data.sendTime);
                        }
                    });
                    break;
                case 'edit':
                    layer.open({
                        type: 2,
                        title: "节目编辑页面",
                        area: ['100%', '100%'],
                        fix: false,
                        maxmin: true,
                        shadeClose: true,
                        shade: 0.4,
                        skin: 'layui-layer-rim',
                        content: ["/system/view/getProgramEdit", "no"],
                        success: function (layero, index) {
                            var body = layer.getChildFrame('body', index);
                            body.find("[name='taskCode']").val(data.taskCode);
                            body.find("[name='stationCode']").val(data.stationCode);
                            body.find("[name='transCode']").val(data.transCode);
                            body.find("[name='taskId']").val(data.taskId);
                            body.find("[name='sendDateTime']").val(data.sendDateTime);
                            body.find("[name='sendDateEndTime']").val(data.sendDateEndTime);
                            body.find("[name='operType']").val(data.operType);
                            body.find("[name='channelFreq']").val(data.channelFreq);
                            body.find("[name='channelRfChState']").val(data.channelRfChState);
                            body.find("[name='channelTimeSlot']").val(data.channelTimeSlot);
                            body.find("[name='channelEqualization']").val(data.channelEqualization);
                            body.find("[name='channelRfChMod']").val(data.channelRfChMod);
                            body.find("[name='channelRfChBand']").val(data.channelRfChBand);
                            body.find("[name='channelRfChASource']").val(data.channelRfChASource);
                            body.find("[name='channelRfChAUrl']").val(data.channelRfChAUrl);
                            body.find("[name='sendTime']").val(data.sendTime);
                        }
                    });
                    break;
                case 'delete':
                    layer.confirm("确认要删除吗，删除后不能恢复", { title: "删除确认" }, function (index) {
                        $.ajax({
                            url: "/system/program/delProgram",
                            type: 'POST',
                            contentType: 'application/json;charset=UTF-8',
                            data: json,
                            success: function (data) {
                                if (data.code === 200) {
                                    popup.success(data.msg);
                                    table.reloadData('myTable');
                                } else {
                                    popup.failure(data.msg);
                                    table.reloadData('myTable');
                                }
                            },
                            error: function () {
                                popup.failure("删除失败!")
                            }
                        });
                    });
                    break;
            }
        });
        function getRecentDay(hour){
            const today = new Date();
            const targetday_milliseconds = today.getTime() + 1000 * 60 * 60 * hour;
            today.setTime(targetday_milliseconds);
            const tYear = today.getFullYear();
            let tMonth = today.getMonth();
            let tDate = today.getDate();
            let tHour = today.getHours();
            let tM = today.getMinutes();
            let ts = today.getSeconds();
            tMonth = doHandleMonth(tMonth + 1);
            tDate = doHandleMonth(tDate);
            return tYear+"-"+tMonth+"-"+tDate + " "+tHour+":" + tM + ":" +ts;
        }
        function doHandleMonth(month){
            let m = month;
            if(month.toString().length === 1){
                m = "0" + month;
            }
            return m;
        }
    })

    function handleFileSelect() {
        var file = document.getElementById('browsefile').files[0];
        var formData = new FormData();
        formData.append('file', file);
        var xhr = new XMLHttpRequest();
        xhr.open("POST", "/system/program/upload", true);
        xhr.onreadystatechange = function() {
            if (xhr.readyState === 4 && xhr.status === 200) {
                // 处理上传成功的响应
            }
        };

        xhr.send(formData);
    }
</script>
</body>

</html>
